<?php	
	include('db_connection.php');

	class add_trainer{
		function new_trainer_add(){				
			$reference_value = $this->createRandomStringForReference();
			$insert_trainer = "INSERT into tbl_admin (Name,email,password,reference_url,skype_mail,contact_number,Address,course,commision,is_admin,is_trainer,is_active) 		VALUES ('".$_POST['user_name']."', '".$_POST['user_email']."', '".md5($_POST['user_pwd'])."', '".$reference_value."', '".$_POST['user_skype_email']."', '".$_POST['user_con_num']."', '".trim($_POST['user_address'])."', '".$_POST['user_course']."','".$_POST['commision']."' ,'".$_POST['is_admin']."', '".$_POST['is_trainer']."', '".$_POST['is_active']."')";	
			$trainer_query = mysql_query($insert_trainer) or die(mysql_error());
			
			$last_id = mysql_insert_id();
			
			$uploaddir = '../dashboard/profile_image/';
			if(!empty($_FILES['user_prof']['name'])){
				
				$temp = explode(".", $_FILES['user_prof']['name']);
				$extension = end($temp);
				$newfilename = $last_id.'.'.end($temp);
				$uploadfile = $uploaddir.$newfilename;
				if (move_uploaded_file($_FILES['user_prof']['tmp_name'], $uploadfile)) {
					$upload_file ="UPDATE tbl_admin SET profile_image='".$_FILES['user_prof']['name']."' WHERE id='".$last_id."'" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}
			/* This funcion is used for  sent mail. ends here */
                        

                        $trainer_name = $_POST['user_email'];
                        $trainer_Password = $_POST['user_pwd'];
                        ob_start();
                        include_once('../../mailtemplate/trainer_login.php');
                        $mail_body = ob_get_contents();
                        ob_end_clean();
                        
			$mail_to=$_POST['user_email'];
			$mail_subject="User Name And Password";
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
			mail($mail_to,$mail_subject,$mail_body, $headers);
			
			$mail_to = 'Vipin7477@gmail.com';
			$mail_subject="User Name And Password";
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
			mail($mail_to,$mail_subject,$mail_body, $headers);
			/* This funcion is used for  sent mail. ends here */
			$success_msg = "Trainer Has been Sucessfully Added";
			return $success_msg;
		}

		function get_trainer($id){
			$select = "SELECT * FROM tbl_admin WHERE id='".$id."'";
			$query = mysql_query($select) or die(mysql_error());
			
			if(mysql_num_rows($query)){
				$results = array();
				while($row = mysql_fetch_array($query)){
					foreach ($row as $k=>$v){
					  $result->$k = $v;
					}
				 
					$results[] = $result;
				}
				return $results;    
			}
		}

		function update_trainer(){			
			$update = "UPDATE tbl_admin SET Name = '".$_POST['user_name']."',
											email = '".$_POST['user_email']."',
											skype_mail = '".$_POST['user_skype_email']."',
											contact_number = '".$_POST['user_con_num']."',
											password = '".md5($_POST['user_pwd'])."',
                                                                                        Address = '".trim($_POST['user_address'])."',
											course = '".$_POST['user_course']."',
											is_admin = '".$_POST['is_admin']."',
                                                                                        commision = '".$_POST['commision']."' ,
											is_trainer = '".$_POST['is_trainer']."',
											is_active = '".$_POST['is_active']."',											
											end_ts = now()
											WHERE id='".$_POST['trainer_id']."'";
			$query_update = mysql_query($update) or die(mysql_error());

			$uploaddir = '../dashboard/profile_image/';
			if(!empty($_FILES['user_prof']['name'])){
				
				$temp = explode(".", $_FILES['user_prof']['name']);
				$extension = end($temp);
				$newfilename = $_POST['trainer_id'].'.'.end($temp);
				$uploadfile = $uploaddir.$newfilename;
				if (move_uploaded_file($_FILES['user_prof']['tmp_name'], $uploadfile)) {
					$upload_file ="UPDATE tbl_admin SET profile_image='".$_FILES['user_prof']['name']."' WHERE id='".$_POST['trainer_id']."'" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}
                        
			/* This funcion is used for  sent mail. ends here */
                        
                         $trainer_name = $_POST['user_email'];
                        $trainer_Password = $_POST['user_pwd'];
                        ob_start();
                        include_once('../../mailtemplate/trainer_login.php');
                        $mail_body = ob_get_contents();
                        ob_end_clean();
                        
                        
			$mail_to=$_POST['user_email'];
			$mail_subject="New Password";
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			$headers .= 'From:Online Sale<info@onlinesalemanagement.com>' . "\r\n";
			mail($mail_to,$mail_subject,$mail_body, $headers);
                        
                        $mail_to ='Vipin7477@gmail.com';
			$mail_subject="New Password";
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
			$headers .= 'From:RV Training<info@rvtraining.net>' . "\r\n";
			mail($mail_to,$mail_subject,$mail_body, $headers);
                        
			/* This funcion is used for  sent mail. ends here */
			$success_msg = "Trainer Has been Sucessfully Updated";
			return $success_msg;
		}

		function del_trainer($trainer_id){
			$del_update = "UPDATE tbl_admin SET is_active = '0', end_ts = now() WHERE id='".$trainer_id."'";			
			$query_update = mysql_query($del_update) or die(mysql_error());
			$success_msg = "Trainer Has been Sucessfully Inactive";
			return $success_msg;			
		}
		
		function createRandomStringForReference(){
			$random_string_final = '';
			$pwd_row = '';
			$alpha_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
			$numeric_arr = array('0','1','2','3','4','5','6','7','8','9');		
			
			$count = 1;
			while($count<2)
			{
				$random_alpha_string = $this->array_random_assoc($alpha_arr,5);
				$random_numeric_string = $this->array_random_assoc($numeric_arr,5);
				$random_string = $random_alpha_string.$random_numeric_string;
				$random_string = str_shuffle($random_string);		
				

				$select_user = "SELECT reference_url FROM tbl_user WHERE reference_url='".$random_string."'";
				$query_user = mysql_query($select_user);
				$row_user = mysql_num_rows ( $query_user );

				$select_admin = "SELECT reference_url FROM tbl_admin WHERE reference_url='".$random_string."'";
				$query_admin = mysql_query($select_admin);
				$row_admin = mysql_num_rows ( $query_admin );

				if(!$row_user && !$row_admin){					
					$count = 2;
					$random_string_final = $random_string;
				}
			}

			if($count == 2){
				return $random_string_final;
			}
		}

		function array_random_assoc($arr, $num = 1) {
			$keys = array_keys($arr);
			shuffle($keys);
			
			$r = '';
			for ($i = 0; $i < $num; $i++) {
				$r .= $arr[$keys[$i]];
			}
			return $r;
		}

	}
?>